package com.service.expert.impl;

import com.dao.expert.NeedsMapper;
import com.entity.Answer;
import com.entity.expert.NeedsDetails;
import com.entity.expert.Processing;
import com.entity.share.Paging;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.expert.NeedsService;
import com.util.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class NeedsServiceImpl implements NeedsService {
    //所有状态
    private String[] STATE = {"pending","toAccept","processing","over"};
    @Autowired
    private NeedsMapper needsMapper;

    public Object queryNeedsAll(Paging paging,String state,Integer experID) {
        Answer answer = new Answer();
        //获取各状态的数量
        List<Integer> stateNums = this.getStateNums(experID);
        //判断需要的状态返回该状态的信息
        if (state.equals(STATE[0])) {
            //分页
            PageHelper.startPage(paging.getPage(), paging.getCurrentSize());
            //获取没人接的需求
            Page<NeedsDetails> needs = (Page<NeedsDetails>) needsMapper.selectNeedsAll(STATE[0], null);
            //拼接返回的数据
            String data = "{\"total\":\"" + needs.getTotal() + "\",\"needs\":" + JSONArray.fromObject(needs) + ",\"states\":" + JSONArray.fromObject(stateNums) + "}";
            answer.setCode(1);
            answer.setData(JSONObject.fromObject(data));
            return answer;
        } else if (state.equals(STATE[1])) {
            PageHelper.startPage(paging.getPage(), paging.getCurrentSize());
            Page<NeedsDetails> needs = (Page<NeedsDetails>) needsMapper.selectNeedsAll(STATE[1], experID);
            String data = "{\"total\":\"" + needs.getTotal() + "\",\"needs\":" + JSONArray.fromObject(needs) + ",\"states\":" + JSONArray.fromObject(stateNums) + "}";
            answer.setCode(1);
            answer.setData(JSONObject.fromObject(data));
            return answer;
        } else if (state.equals(STATE[2])) {
//            PageHelper.startPage(paging.getPage(), paging.getCurrentSize());
//            Page<NeedsDetails> needs = (Page<NeedsDetails>) needsMapper.selectNeedsAll(STATE[2], experID);
//            String data = "{\"total\":\"" + needs.getTotal() + "\",\"needs\":" + JSONArray.fromObject(needs) + ",\"states\":" + JSONArray.fromObject(stateNums) + "}";
//            answer.setCode(1);
//            answer.setData(JSONObject.fromObject(data));
//            return answer;
           //分页
            PageHelper.startPage(paging.getPage(), paging.getCurrentSize());
            //在数据库中获取咨询类表数据
            Page<Processing> processings = (Page<Processing>)needsMapper.queryProcessings(experID,state);
            //拼接需要的数据
            String data = "{\"total\":\"" + processings.getTotal() + "\",\"needs\":" + JSONArray.fromObject(processings) + ",\"states\":" + JSONArray.fromObject(stateNums) + "}";
            answer.setCode(1);
            answer.setData(JSONObject.fromObject(data));
           return answer;
        } else if (state.equals(STATE[3])){
            PageHelper.startPage(paging.getPage(), paging.getCurrentSize());
            Page<NeedsDetails> needs = (Page<NeedsDetails>) needsMapper.selectNeedsAll(STATE[3], experID);
            String data = "{\"total\":\"" + needs.getTotal() + "\",\"needs\":" + JSONArray.fromObject(needs) + ",\"states\":" + JSONArray.fromObject(stateNums) + "}";
            answer.setCode(1);
            answer.setData(JSONObject.fromObject(data));
            return answer;
        }else{
            answer.setCode(0);
            answer.setError("参数错误！");
            return answer;
        }

    }
    @Transactional
    public Object expertsAcceptance(Integer needsID,Integer expertID,String state){
        Answer answer = new Answer();
        if (state.equals(STATE[2])){
            try {
                if (needsMapper.updateNeedsState(needsID,expertID,state, Util.getDate())==1 && needsMapper.upadteCasesState(needsID,expertID,state) == 1 && needsMapper.addAdvisory(needsID) == 1){
                    answer.setCode(1);
                    answer.setData(JSONArray.fromObject(this.getStateNums(expertID)));
                    return answer;
                }else {
                    answer.setCode(0);
                    answer.setError("操作失败！");
                    return answer;
                }
            }catch (Exception e){
                e.printStackTrace();
                answer.setCode(0);
                answer.setError("操作失败！");
                return answer;
            }
        }else {
            answer.setCode(0);
            answer.setError("参数错误！！");
            return answer;
        }
    }


    /**
     * 获取个状态的数量
     */
    private List<Integer> getStateNums(Integer experID){
        List<Integer> stateNums = new ArrayList<Integer>();
        stateNums.add(needsMapper.queryNeedStateByState(STATE[0], null));
        stateNums.add(needsMapper.queryNeedStateByState(STATE[1], experID));
        stateNums.add(needsMapper.queryNeedStateByState(STATE[2], experID));
        stateNums.add(needsMapper.queryNeedStateByState(STATE[3], experID));
        return stateNums;
    }
}
